<template>
  <div>
    <ButtonGroup id="appp">
            <Button type="primary" @click="type1">新闻分类</Button>
            <Button type="primary" @click="type2">产品分类</Button>
            </ButtonGroup>
    <Tree :data="data7" :render="renderContent"></Tree>
    <Modal
        v-model="modal1"
        title="数据管理"
        @on-ok="ok"
        @on-cancel="cancel">
            <Input v-model="title" placeholder="请输入标题"></Input>
    </Modal>
    
    </div>
    
</template>
<script>
export default {
  data() {
    return {
      modal1: false,
      type: 1,
      title:'',
      parentId:'',
      data7: [
        {
          title: "根目录",
          expand: true,
          render: (h, { root, node, data }) => {
            return h(
              "span",
              {
                style: {
                  display: "inline-block",
                  width: "100%"
                }
              },
              [
                h("span", [
                  h("Icon", {
                    props: {
                      type: "ios-folder-outline"
                    },
                    style: {
                      marginRight: "8px"
                    }
                  }),
                  h("span", data.title)
                ]),
                h(
                  "span",
                  {
                    style: {
                      display: "inline-block",
                      float: "right",
                      marginRight: "32px"
                    }
                  },
                  [
                    h("Button", {
                      props: Object.assign({}, this.buttonProps, {
                        icon: "ios-plus-empty",
                        type: "primary"
                      }),
                      style: {
                        width: "52px"
                      },
                      on: {
                        click: () => {
                          this.append(data);
                        }
                      }
                    })
                  ]
                )
              ]
            );
          },
          children: []
        }
      ],
      buttonProps: {
        type: "ghost",
        size: "small"
      }
    };
  },
  methods: {
    newpro(){
      console.log(1)
    },
    type1() {
      this.type = 1;
      this.getTree();
    },
    type2() {
      this.type = 2;
      this.getTree();
    },  
    renderContent(h, { root, node, data }) {
      return h(
        "span",
        {
          style: {
            display: "inline-block",
            width: "100%"
          }
        },
        [
          h("span", [
            h("Icon", {
              props: {
                type: "ios-paper-outline"
              },
              style: {
                marginRight: "8px"
              }
            }),
            h("span", data.title)
          ]),
          h(
            "span",
            {
              style: {
                display: "inline-block",
                float: "right",
                marginRight: "32px"
              }
            },
            [
              h("Button", {
                props: Object.assign({}, this.buttonProps, {
                  icon: "ios-plus-empty"
                }),
                style: {
                  marginRight: "8px"
                },
                on: {
                  click: () => {
                    this.append(data);
                  }
                }
              }),
              h("Button", {
                props: Object.assign({}, this.buttonProps, {
                  icon: "ios-minus-empty"
                }),
                on: {
                  click: () => {
                    this.remove(root, node, data);
                  }
                }
              })
            ]
          )
        ]
      );
    },
    getTree() {
      this.axios({
        method: "get",
        url: `http://10.31.155.73:3000/cate/list/${this.type}`,
        // data: this.filter
      }).then(response => {
        // console.log(response.data);
        this.data7[0].children = response.data;
      });
    },
    append(data) {
      this.modal1 = true;
      this.axios({
        method: "get",
        url: `http://10.31.155.73:3000/cate/list/${this.type}`,
      }).then(response => {
        // console.log(response.data);
        this.parentId = data._id;
      });
      // children.push({
      //   title: this.title,
      //   expand: true
      // });
    },
    remove(root, node, data) {
      this.$Modal.confirm({
                title: "确认删除？",
                content: "请确认是否删除该记录！",
                onOk: () => {
                    this.axios({
                        method: "delete",
                        url: `http://10.31.155.73:3000/cate/data/${data._id}`,
                    }).then(res => {
                        this.$Message.info("删除成功");
                        this.getTree();
                    });
                }
            });
    },
    ok() {
      this.axios({
        method: "POST",
        url: `http://10.31.155.73:3000/cate/data`,
        data:{
          title:this.title,
          parentId:this.parentId,
          type:this.type
        }
      }).then(response => {
        this.modal1 = false;
        this.getTree()
      });
      this.$Message.info("Clicked ok");
    },
    cancel() {
      this.$Message.info("Clicked cancel");
    }
  },
  mounted() {
    this.getTree();
  }
};
</script>
